/**
 * 
 */
package com.wqb.interceptor;

import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.wqb.application.Application;
import com.wqb.until.LogUtils;

/**
 * @author wangqb
 *
 */
public class UserLoginVerifyInterceptor extends AbstractInterceptor {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1177084845364322485L;
	@Autowired
	private LogUtils logUtils;
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		Map session = invocation.getInvocationContext().getSession();
		Object currentUser = session.get(Application.USER);
		if (null != currentUser) {
			log(invocation);
			return invocation.invoke();
		} else {
			return Action.LOGIN;
		}
	}
	
	private void log(ActionInvocation ai){
		Map<String, Object> map = ai.getInvocationContext().getParameters();
		String action = ai.getProxy().getActionName();
		String method = ai.getProxy().getMethod();
		String nameSpace = ai.getProxy().getNamespace();
		String url = nameSpace+"/"+action+"!"+method+".do";
		StringBuffer params = new StringBuffer();
		Set<String> set = map.keySet();
		for(String str :set){
			Object[] value = (Object[]) map.get(str);
			params.append(str+"="+value[0]+"&");
		}
		if(StringUtils.isNotBlank(params.toString())){
			url += "?"+params.substring(0,params.lastIndexOf("&"));
		}
		logUtils.sysLogForFile("RequestUrl:"+url);
	}
}
